package com.liholin.springboot.sample.controller

import com.liholin.springboot.sample.bean.LoginBean
import com.liholin.springboot.sample.bean.Member
import com.liholin.springboot.sample.configdata.ResponseData
import com.liholin.springboot.sample.service.MemberService
import com.liholin.springboot.sample.utils.OkHttpUtil
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import javax.servlet.http.HttpServletRequest

@RestController
@RequestMapping("userInfo")
@Slf4j
class MemberInfoController {

    @Autowired
    private val memberService: MemberService? = null

    @Value("\${newshop.osType.app}")
    private val newShopApp: String? = null
    //两种实现方式 启动下面的方式获取bean 需要将 MemberService的实现类@Serivce注解去掉
//    @Resource(name = "memberServiceImplBeanAction")
//    private val memberServiceBeanAction: MemberService? = null

    @GetMapping("sortMember")
    fun sortMember(@RequestParam("page") page: Int,
                   @RequestParam(value = "pageSize", required = false, defaultValue = "10") pageSize: Int,
                   @RequestParam(value = "desc", required = false, defaultValue = "0") desc: Int,
                   @RequestParam(value = "sort", required = false) sort: String,
                   @RequestParam(value = "query", required = false) query: String?): ResponseData {
        println("===========${memberService!!.sqlMontage()}")
        return ResponseData.ok(memberService!!.getSortMember(page, pageSize, desc, sort, query))
    }

    @GetMapping("sortMemberList")
    fun sortMemberList(@RequestParam("page") page: Int,
                       @RequestParam(value = "pageSize", required = false, defaultValue = "10") pageSize: Int,
                       @RequestParam(value = "desc", required = false, defaultValue = "0") desc: Int,
                       @RequestParam(value = "sort", required = false, defaultValue = "1") sort: String,
                       @RequestParam(value = "query", required = false) query: String?): ResponseData {
        return ResponseData.ok(memberService!!.getSortMemberList(page, pageSize, desc, sort, query))
    }

    @GetMapping("sortServicePageList")
    fun sortServicePageList(@RequestParam("page") page: Int,
                            @RequestParam(value = "pageSize", required = false, defaultValue = "10") pageSize: Int,
                            @RequestParam(value = "desc", required = false, defaultValue = "0") desc: Int,
                            @RequestParam(value = "sort", required = false, defaultValue = "1") sort: String,
                            @RequestParam(value = "query", required = false) query: String?): ResponseData {
        return ResponseData.ok(memberService!!.selectServicePage(page, pageSize, desc, sort, query))
    }

    @PostMapping("saveMember")
    fun saveMember(@RequestBody member: Member): ResponseData? {
        return memberService?.saveMember(member)
    }

    @PostMapping("saveMemberObj")
    fun saveMemberObj(@RequestBody request: Map<String,Any>): ResponseData? {
        return if (request.isEmpty() || !request.containsKey("name"))
            ResponseData.error("请求参数错误")
            else ResponseData.ok(data = request["name"])
    }

    @PostMapping("login")
    fun saveMember(@Validated @RequestBody loginBean: LoginBean, @RequestParam app: String): ResponseData? {
        println("三方访问URL：" + OkHttpUtil.get("http://localhost:3096/v0/api/userInfo/sortMember?page=1&desc=2&sort=age&query=%7B%22memberId%22%3A1%2C%22name%22%3A%22AndyM%22%7D&app=newshopApp", null, null))
        println("时间：${LocalDate.now().format(DateTimeFormatter.ofPattern("dd"))}")
        return memberService?.login(loginBean, app)
    }

    @GetMapping("getMemberListJetCache")
    fun getMemberListJetCache(@RequestParam(value = "id") id: Int, httpServletRequest: HttpServletRequest): ResponseData {
        println("=========${httpServletRequest.getHeader("userId")}")
        return ResponseData.ok(memberService?.getMemberListJetCache(id))
    }

    @PutMapping("updateMemberListJetCache")
    fun updateMemberListJetCache(@RequestBody member: Member): ResponseData? {
        return memberService?.updateMemberListJetCache(member)
    }

    @DeleteMapping("deleteMemberListJetCache")
    fun deleteMemberListJetCache(@RequestParam(value = "id") id: Int): ResponseData {
        return ResponseData.ok(memberService?.deleteMemberListJetCache(id))
    }

    @GetMapping("getUserId")
    fun getUserId(httpServletRequest: HttpServletRequest): ResponseData {
        return ResponseData.ok(httpServletRequest.getHeader("userId"))
    }
}